/*
 * @brief: 
 * @Descripttion: 
 * @Date: 2021-03-08 20:35:14
 * @LastEditTime: 2021-03-08 20:35:45
 */
/******************************************************************************
 * Copyright (C) 2014-2020 Zhifeng Gong <gozfree@163.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 ******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include "rbtree_queue.h"

// #define container_of(ptr, type, member) ({			\
// 	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
// 	(type *)( (char *)__mptr - offsetof(type,member) );})

#ifndef false
#define false   (0)
#endif

#ifndef true
#define true    (!(false))
#endif

struct __device_list{
    char device_id[32];
    char location[32];
}device_list_t,*device_list_ret_t;


rbtree_queue_t g_rb_queue_devicelist;
int  test_rb_queue(rbtree_queue_ref_t p_queue, char *input[], size_t len)
{
    rbqueue_node_t *p_queue_node;
    int i,result;

    printf("\tinput len:%d\n",(int)len);
    RB_QUEUE_LOCK(p_queue);
    for (i = 0; i < (int)len; i++) {
        result = rbtree_queue_insert(p_queue, input[i], input[i]);
        printf("insert %d: %s, result %d\n", i, input[i], result);
    }
    RB_QUEUE_UNLOCK(p_queue);

    printf("->->->->->->->size:%d->->->->->->->->->->->\n", RB_QUEUE_SIZE(p_queue));
    p_queue_node = rbtree_queue_first_node(p_queue);
    i = 0;
    while (p_queue_node) {
        printf("%d: %s\n", i++, (char *)(p_queue_node->priv_data));
        p_queue_node = rbtree_queue_next_node(p_queue_node);
    }

    printf("<-<-<-<-<-<-<-<size:%d-<-<-<-<-<-<-<-<-<-<-\n", RB_QUEUE_SIZE(p_queue));
    p_queue_node = rbtree_queue_last_node(p_queue);
    i = 0;
    while (p_queue_node) {
        printf("%d: %s\n", i++, (char *)(p_queue_node->priv_data));
        p_queue_node = rbtree_queue_prev_node(p_queue_node);
    }

    i = 0;

    char *p;
    p =  rbtree_queue_search(p_queue,"111");
    if(p){
        printf("\tsearch 111  %s\n",p);
    }
    p =  rbtree_queue_search(p_queue,"123");
    if(p){
        printf("\tsearch 123  %s\n",p);
    }

    printf("\tstart erase\n");
    i = 0;
    while (!RB_QUEUE_EMPTY_ROOT(p_queue)) {
        p_queue_node = rbtree_queue_first_node(p_queue);
        printf("%d: %s\n", i++, (char *)(p_queue_node->priv_data));
        rbtree_queue_erase(p_queue,(p_queue_node->priv_data));
    }
    printf("\tend erase size:%d\n", RB_QUEUE_SIZE(p_queue));

    return 0;
}
static char *input1[] = {"11","22","12","11","54","33","9987","123"};

int rb_tree_test_main()
{
    rbtree_queue_init(&g_rb_queue_devicelist);

    printf("input 1=====================\n");
    test_rb_queue(&g_rb_queue_devicelist, input1, (sizeof(input1)/sizeof(input1[0])));

    return 0;
}